<script setup lang="ts">
import { getHomeBannerAPI, getCategoryAPI, getHotPanelAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app' // 小程序onLoad导入地址
import { ref } from 'vue'
import type { BannerItem, CategoryList, HotPanelList } from '@/types/home'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/pageSkeleton.vue'
import type { XtxGuessInstance } from '@/components/components'
import { useGuessList } from '@/composables'

// 请求接口 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
// 请求获取分类数据
const categoryList = ref<CategoryList[]>([])
const getCategoryData = async () => {
  const res = await getCategoryAPI()
  categoryList.value = res.result
}
// 热门推荐
const hotPanelList = ref<HotPanelList[]>([])
const getHotPanelList = async () => {
  const res = await getHotPanelAPI()
  hotPanelList.value = res.result
}
// 猜你喜欢  滚动触底加载更多
// const guessRef = ref<XtxGuessInstance>()
// const toScrolltolower = () => {
//   guessRef.value?.getGuessData()
// }
const { guessRef, onScrolltolower } = useGuessList()
const refresherTriggered = ref(false)
const onRefresh = async () => {
  refresherTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getCategoryData(),
    getHotPanelList(),
    guessRef.value?.getGuessData(),
  ])
  refresherTriggered.value = false
}
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryData(), getHotPanelList()])
  isLoading.value = false
})
// 滚动时 获取滚动距离
const scrollDis = ref(0)
const onScroll: UniHelper.ScrollViewOnScroll = (e) => {
  scrollDis.value = e.detail.scrollTop
}
const toTop = () => {
  scrollDis.value = 0
}
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar id="navbarc" />
  <!-- <XtxTotop :scrollDis="scrollDis" /> -->
  <view class="scrollTopView" @tap="toTop" v-if="scrollDis > 500">
    <text class="XTXIconfont icon-fanhuidingbu1"></text>
  </view>
  <scroll-view
    scroll-y
    class="scrollView"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresh"
    :refresher-triggered="refresherTriggered"
    @scroll="onScroll"
    :scroll-top="scrollDis"
  >
    <!-- 骨架屏 -->
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮播图组件 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类组件 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotPanelList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
@import '@/static/iconfont/iconfont.css';

page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scrollView {
  flex: 1;
}
.scrollTopView {
  position: fixed;
  right: 50rpx;
  bottom: 50rpx;
  z-index: 999;
  width: 60rpx;
  height: 60rpx;
}
.icon-fanhuidingbu1 {
  font-size: 75rpx;
}
</style>
